package com.liukai.bishe.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

    /**
     * 时间戳转换为日期
     * @param format
     * @param time
     * @return
     */
    public static String timeMillisToTime(String format,String time){
        long timeLong = Long.valueOf(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(timeLong);
        return simpleDateFormat.format(date);
    }

    public static String getTotalTime(String format,String startTimeMillis,String stopTimeMillis){

        String startTime = timeMillisToTime("yyyy-MM-dd HH:mm:ss",startTimeMillis);
        String endTime = timeMillisToTime("yyyy-MM-dd HH:mm:ss",stopTimeMillis);
        //看自己的时间格式选择对应的转换对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        //转换成date类型
        Date start = null;
        try {
            start = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date end = null;
        try {
            end = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取毫秒数
        Long startLong = start.getTime();
        Long endLong = end.getTime();
        //计算时间差,单位毫秒
        Long ms = endLong-startLong;
        //时间差转换为 \天\时\分\秒
        String time = longTimeToDay(ms);
        return time;

    }

    public static String longTimeToDay(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
        if(milliSecond > 0) {
            sb.append(milliSecond+"毫秒");
        }
        return sb.toString();
    }

    public static String longTimeToHours(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
        if(milliSecond > 0) {
            sb.append(milliSecond+"毫秒");
        }
        return sb.toString();
    }

    /**
     * 时间差转换为分钟
     * @param ms
     * @return
     */
    public static String longTimeToMinuties(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

//        StringBuffer sb = new StringBuffer();
//        if(day > 0) {
//            sb.append(day+"天");
//        }
//        if(hour > 0) {
//            sb.append(hour+"小时");
//        }
//        if(minute > 0) {
//            sb.append(minute+"分");
//        }
//        if(second > 0) {
//            sb.append(second+"秒");
//        }
//        if(milliSecond > 0) {
//            sb.append(milliSecond+"毫秒");
//        }
        return String.valueOf(ms/(1000*60));
    }

}
